package com.system.controller;

import com.order.hit.crossover.platform.common.utils.PFResponse;
import com.system.mapper.UserMapper;
import com.order.hit.crossover.platform.common.bean.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Lei
 * @version 0.1
 * @date 2020/9/8
 */
@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    private UserMapper userMapper;

    @PostMapping("/login")
    public PFResponse login(@RequestBody User user){
        User user1 = this.userMapper.login(user.getUserName(), user.getUserPassword());
        if (user1 == null){
            return new PFResponse().code(500).message("登陆失败").put("login", false);
        }
        return new PFResponse().code(200).message("登陆成功").put("login", true).data(user1);
    }

    @PostMapping("/register")
    public PFResponse register(@RequestBody User user){
        PFResponse response = new PFResponse();
        try {
            this.userMapper.insertUser(user);
            response.code(200);
            response.message("注册成功");
        }catch (Exception e){
            response.code(500);
            response.message("注册失败");
        }
        return response;
    }

    @GetMapping("/checkuserName/{userName}")
    public PFResponse checkUsernane(@PathVariable("userName")String userName){
        PFResponse response = new PFResponse();
        User user = this.userMapper.checkuserName(userName);
        response.code(200);
        if (user == null){
            response.message("没有此用户");
            response.put("ifExit", false);
        } else{
            response.message("有此用户");
            response.put("ifExit", true);
            response.data(user);
        }
        return response;
    }


    @GetMapping("/getuserByid/{id}")
    public PFResponse getUserByid(@PathVariable("id")int id){
        PFResponse response = new PFResponse();
        User user = this.userMapper.getUserByid(id);
        response.code(200);
        if (user == null){
            response.message("没有此用户");
            response.put("ifExit", false);
        } else{
            response.message("有此用户");
            response.put("ifExit", true);
        }
        response.data(user);
        return response;
    }
}

